package com.example.ceramics.database.dao;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.ceramics.database.DatabaseContants;
import com.example.ceramics.database.DatabaseHelper;
import com.example.ceramics.entity.CartEntity;
import com.example.ceramics.entity.GoodsEntity;

import java.util.ArrayList;
import java.util.List;

public class CartDaoImpl implements ICartDao {

    private final DatabaseHelper databaseHelper;

    public CartDaoImpl(Context context) {
        databaseHelper = new DatabaseHelper(context);
    }

    @Override
    public int addCart(int goodsId, String account, double goodsPrice) {
        if (!hasCart(goodsId,account)) {
            SQLiteDatabase db = databaseHelper.getWritableDatabase();
            String sql = "insert into " + DatabaseContants.TABLE_CART +
                    "(account,goodsId,num,price) values(?,?,?,?)";
            db.execSQL(sql, new Object[]{account, goodsId, 1, goodsPrice});
            db.close();
            return 1;
        }
//        else {
//            String sql = "update "+DatabaseContants.TABLE_CART +" set num=num+1 where goodsId=? and account=?";
//            db.execSQL(sql,new Object[]{goodsId,account});
            updateNum(goodsId,account,1,goodsPrice);
//        }
        return 2;
    }

    @Override
    public int deleteCart(int goodsId, String account) {
        if(!hasCart(goodsId,account)){
            return -1;
        }
        SQLiteDatabase db = databaseHelper.getWritableDatabase();
        String sql = "delete from "+DatabaseContants.TABLE_CART + " where goodsId=? and account=?";
        db.execSQL(sql,new Object[]{goodsId,account});
        db.close();
        return 1;
    }

    @Override
    public int deleteCart(List<CartEntity> cartList) {
        if(cartList==null||cartList.size()==0){
            return -1;
        }
        String sql = "delete from "+DatabaseContants.TABLE_CART + " where goodsId = ? and account =?";
        SQLiteDatabase db = databaseHelper.getWritableDatabase();
        for (CartEntity cart : cartList) {
            db.execSQL(sql,new Object[]{cart.getGoodsId(), cart.getAccount()});
        }
        db.close();
        return 1;
    }

    @Override
    public int updateNum(int goodsId, String account,int addNum,double goodsPrice) {
        if(!hasCart(goodsId,account)){
            return -1;
        }
        SQLiteDatabase db = databaseHelper.getWritableDatabase();
        String sql = "update "+DatabaseContants.TABLE_CART +" set num=num+?,price=price+? where goodsId=? and account=?";
        db.execSQL(sql,new Object[]{addNum,addNum*goodsPrice,goodsId,account});
        db.close();
        return 1;
    }

    @Override
    public boolean hasCart(int goodsId, String account) {
        SQLiteDatabase db = databaseHelper.getReadableDatabase();
        String sql = "select goodsId from " + DatabaseContants.TABLE_CART + " where goodsId=? and account=?";
        Cursor cursor = db.rawQuery(sql, new String[]{"" + goodsId, account});
        if (cursor.moveToFirst()) {
            cursor.close();
            db.close();
            return true;
        }
        cursor.close();
        db.close();
        return false;
    }

    @Override
    public boolean isEmpty(String account) {
        SQLiteDatabase db = databaseHelper.getReadableDatabase();
        String sql = "select * from "+DatabaseContants.TABLE_CART + " where account=? LIMIT 1";
        Cursor cursor = db.rawQuery(sql, new String[]{account});
        if (cursor.moveToFirst()) {
            cursor.close();
            db.close();
            return false;
        }
        cursor.close();
        db.close();
        return true;
    }

    @Override
    public List<CartEntity> getCartList(String account) {
        SQLiteDatabase db = databaseHelper.getReadableDatabase();
        String sql = "select * from " + DatabaseContants.TABLE_CART+" join "+DatabaseContants.TABLE_GOODS +
                " on " +DatabaseContants.TABLE_CART+".goodsId="+DatabaseContants.TABLE_GOODS+".goodsId where account=?";
        Cursor cursor = db.rawQuery(sql, new String[]{account});
        List<CartEntity> cartList = new ArrayList<>();
        while (cursor.moveToNext()) {
            cartList.add(cursorToCart(cursor));
        }
        cursor.close();
        db.close();
        return cartList;
    }

    private CartEntity cursorToCart(Cursor cursor) {
        GoodsEntity goods;
        int goodsId = cursor.getInt(cursor.getColumnIndex("goodsId"));
        String goodsName = cursor.getString(cursor.getColumnIndex("goodsName"));
        double goodsPrice = cursor.getDouble(cursor.getColumnIndex("goodsPrice"));
        String goodsSignature = cursor.getString(cursor.getColumnIndex("goodsSignature"));
        String goodsType = cursor.getString(cursor.getColumnIndex("goodsType"));
        int goodsImg = cursor.getInt(cursor.getColumnIndex("goodsImg"));
        goods = new GoodsEntity(goodsId, goodsName, goodsPrice, goodsSignature, goodsType, goodsImg);

        String account = cursor.getString(cursor.getColumnIndex("account"));
        int num = cursor.getInt(cursor.getColumnIndex("num"));
        double price = cursor.getDouble(cursor.getColumnIndex("price"));
        CartEntity cart = new CartEntity(account,goodsId,num,price,goods);
        return cart;
    }
}
